﻿using System.Collections;
using UnityEngine;
using System;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using IMIForUnity;


namespace IMIForUnity
{	
	public  partial class BodyMeaMgrImp
	{

		float chestRatio = 1f; //0.88f;      //  0.98f;
		public float chestWidth = 0;
		public float chestThickness = 0f;

		public float chestWidthtmp = 0f;

//		public ClusterSmoothTool chestSmthWideness;
//		public ClusterSmoothTool chestSmthThickness;

		public ArrayList chestSmthWidthArr=new ArrayList();
		public ArrayList chestSmthThicknessArr=new ArrayList();
		
		public void ProcessChest (int userID, bool isDrawOnDepthMap)
		{
			GetChestFrontWidth(userID, isDrawOnDepthMap);

			if(isDrawOnDepthMap)
			{
				DrawJointsMarksOnDepthMap(userID, ImiSkeleton.Index.SHOULDER_LEFT
				                          ,5,255,255,0,255);
					
				DrawJointsMarksOnDepthMap(userID,ImiSkeleton.Index.SHOULDER_RIGHT
				                          ,5,255,255,0,255);

			}		
		}


		void GetChestFrontWidth(int userID, bool isDrawOrNot)
		{ 

			//before  11 25
//			Vector3 leftshoulderPos=GetJointSpacePos( userID,  
//			       ImiSkeleton.Index.SHOULDER_LEFT);
//			Vector3 rightshoulderPos=GetJointSpacePos( userID,  
//			       ImiSkeleton.Index.SHOULDER_RIGHT);
//
//			chestWidthtmp=Mathf.Abs(leftshoulderPos.x - rightshoulderPos.x  )*1.3f; 
			//			chestSmthWideness.InsertValue(chestWidetmp);
//			chestWidth=chestSmthWideness.GetFilteredValue();


			//In 11 25
//			ImiWrapper.ImiVector4 lShoulderjointPosV4 =
//				playerInfos [userID].
//					jointsPosV4[(int)ImiWrapper.ImiSkeleton.Index.SHOULDER_LEFT];
//			
//
//			int lx=0;
//			int ly=0;
//			int lz=0;
//			
//			ImiWrapper.ConvertSkeletonPointToDepthPoint (
//				lShoulderjointPosV4,
//				ref lx,
//				ref ly,
//				ref lz,
//				ImiResolution.Type._640x480);
//			
//			
//			lx=RangeValue(lx,0,DEPTHWIDTH);
//			ly=RangeValue(ly,0,DEPTHHEIGHT);
//			int shoulderDepth=depthPlayerDataOriginal[ ly*DEPTHWIDTH+lx]>>3;
//			
//			chestWidthtmp=GetThicknessFromOnePixPos(depthPlayerDataOriginal,
//			                                       DEPTHWIDTH, 
//			                                       DEPTHHEIGHT, 
//			                                       lx,
//			                                       ly,
//			                                       boundThreshval,			               
//			                                       shoulderDepth, 
//			                                       isDrawOrNot)*0.85f;




			//Since  12 01 3rd way to test chest width
			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;
			
			
			int shoulderMidPixPosX=0;
			int shoulderMidPixPosY=0;
			int shoulderMidPixPosZ=0;
			
			GetPlayerJointPixPos (userID, ref shoulderMidPixPosX, ref shoulderMidPixPosY, ref shoulderMidPixPosZ, 
			                     ImiSkeleton.Index.SHOULDER_LEFT,true);
			
			int spineX=0;
			int spineY=0;
			int spineZ=0;
			
			GetPlayerJointPixPos (userID, ref spineX, ref spineY, ref spineZ, 
			                     ImiSkeleton.Index.SPINE,true);
			
			
			//To measure the hips thickness.
			int chestupx=RangeValue(spineX, 0 ,dw);;
			//int hipssideupy=RangeValue((hipsMidPixPosY+(hipsMidPixPosY-hipleftY)*2),0 ,dh); 
			int chestupy=RangeValue(shoulderMidPixPosY,0 ,dh); 
			
			
			int chestdownX=RangeValue(spineX, 0 ,dw);
			int chestdownY=RangeValue((spineY+shoulderMidPixPosY)/2, 0 ,dh);
			
			
			
			
			Vector2 chestUpPixPos = new Vector2(chestupx,chestupy );
			Vector2 chestDownPixPos = new Vector2(chestdownX,chestdownY );
			
			
			//we add a adjusting deeping value on the waist surface depth.
			
			//method 1 get the thicknest part of the hips side	by using 2 pixposes
			int tempPlayerIndex0 = depthPlayerDataOriginal  [chestupy *dw +chestupx] & 7;
			int tempPlayerIndex1 = depthPlayerDataOriginal  [chestdownY *dw +chestdownX] & 7;
			
			
			//Only when the key pix pos symplify an player pixel.
			if((tempPlayerIndex0>0)&&(tempPlayerIndex1>0)&&(tempPlayerIndex0== tempPlayerIndex1 ))
			{
				
				chestWidthtmp=GetThicknessFromPixPosesArr(dw,dh,depthPlayerDataOriginal,
				                                               boundThreshval,chestUpPixPos,chestDownPixPos,
				                                               adjustDeepingLengthForSideWaist, isDrawOrNot)*0.85f;	
				
				
				//Debug.Log("+++++++++++++++++++++++++++hips side tmep: "+hipsSideTmp.ToString());
				//					hipsSmthThickness.InsertValue (hipsSideTmp);
				//					hipsThickness=hipsSmthThickness.GetFilteredValue();

			}
			


			chestSmthWidthArr.Add(chestWidthtmp);
			//return chestWidth;
		}





		void ProcessChestSide(int userID, bool isDrawOrNot)
		{
			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;

			int shoulderMidPixPosX=0;
			int shoulderMidPixPosY=0;
			int shoulderMidPixPosZ=0;

			GetPlayerJointPixPos (userID, ref shoulderMidPixPosX, ref shoulderMidPixPosY, ref shoulderMidPixPosZ, 
			                     ImiSkeleton.Index.SHOULDER_CENTER,false);

			int spineMidPixPosX=0;
			int spineMidPixPosY=0;
			int spineMidPixPosZ=0;
						
			GetPlayerJointPixPos (userID, ref spineMidPixPosX, ref spineMidPixPosY, ref spineMidPixPosZ, 
			                     ImiSkeleton.Index.SPINE,false);

			chestPixPosX=(shoulderMidPixPosX+spineMidPixPosX)/2;
			chestPixPosY=(shoulderMidPixPosY+spineMidPixPosY)/2;
			chestPixPosZ=(shoulderMidPixPosZ+spineMidPixPosZ)/2;

			///method before 11 22. use the middle point between shoulder center and spine as the chest position 
			/// and use that positon as the only position of chest.

//
//			int tempPlayerIndex = depthPlayerDataOriginal [chestPixPosY *w +chestPixPosX] & 7;
//
//			//Only when the key pix pos symplify an player pixel.
//			if(tempPlayerIndex>0)
//			{
//
//			    int ChestSideDepth=((depthPlayerDataOriginal [chestPixPosY *w +chestPixPosX]) >> 3)+adjustDeepingLengthForSideChest;
//				float chestSideTmp=GetThicknessFromOnePixPos(depthPlayerDataOriginal, 
//				                                             w, 
//				                                             h, 
//				                                             chestPixPosX,
//				                                             chestPixPosY,
//				                                             boundThreshval,			               
//				                                             ChestSideDepth, 
//				                                             isDrawOrNot);			
////				chestSmthThickness.InsertValue (chestSideTmp);
////				chestThickness = chestSmthThickness.GetFilteredValue () ;
//				chestSmthThicknessArr.Add(chestSideTmp);
//			}


///method after 11 22. Use an range between shoulder center and spine.

			int chestsideupx=RangeValue(chestPixPosX, 0 ,dw);
			int chestsideupy=RangeValue(chestPixPosY+(int)(((float)(shoulderMidPixPosY-chestPixPosY))/3)*2, 0 ,dh);
			int chestsidedownx=RangeValue(chestPixPosX, 0 ,dw);;
			int chestsidedowny=RangeValue(chestPixPosY+(int)(((float)(spineMidPixPosY-chestPixPosY))/4)*3, 0 ,dh); 

			Vector2 ChestUpPixPos = new Vector2(chestsideupx,chestsideupy );
			Vector2 ChestDownPixPos = new Vector2(chestsidedownx,chestsidedowny );
			
			
			//we add a adjusting deeping value on the waist surface depth.
			
			//method 1 get the thicknest part of the hips side	by using 2 pixposes
			int tempPlayerIndex0 = depthPlayerDataOriginal  [chestsideupy *dw +chestsideupx] & 7;
			int tempPlayerIndex1 = depthPlayerDataOriginal  [chestsidedowny *dw +chestsidedownx] & 7;

			if((tempPlayerIndex0>0)&&(tempPlayerIndex1>0)&&(tempPlayerIndex0== tempPlayerIndex1 ))
			{
				
				float chestSideTmp=GetThickestPartBy2PixPoses(dw,dh,depthPlayerDataOriginal,
				                                             boundThreshval,ChestUpPixPos,ChestDownPixPos,isDrawOrNot);				
				//Debug.Log("+++++++++++++++++++++++++++hips side tmep: "+hipsSideTmp.ToString());
				//					hipsSmthThickness.InsertValue (hipsSideTmp);
				//					hipsThickness=hipsSmthThickness.GetFilteredValue();
				
				chestSmthThicknessArr.Add(chestSideTmp);
			}
		}



		public float  GetChestGirth( )
		{
			return IMIBodyMeaTool.CalculateChestGirth(chestWidth, chestThickness,chestRatio);

		}



//		public void GetChestSideThickness(ushort[] _depthPlayerDataOriginal, 
//		                                  int depthW, 
//		                                  int depthH, 
//		                                  int chestPixPosX,
//		                                  int chestPixPosY,
//		                                  ushort boundThreshval,
//		                                  int depthVal,
//		                                  bool isDrawOrNot
//		                                  )
//		{
//
//		
//			//we add a adjusting deeping value on the waist surface depth.
//			int ChestSideDepth=((_depthPlayerDataOriginal [chestPixPosY *depthW +chestPixPosX]) >> 3)+adjustDeepingLengthForSideChest;
//			
//			float chestSideTmp=GetThicknessFromOnePixPos(_depthPlayerDataOriginal, 
//			                          w, 
//			                          h, 
//			                          chestPixPosX,
//			                          chestPixPosY,
//			                          boundThreshval,			               
//			                          ChestSideDepth, 
//			                          isDrawOrNot);
//
//			chestSmthThickness.InsertValue (chestSideTmp);
//			chestThickness = chestSmthThickness.GetFilteredValue () * chestThicknessRatio;
//
//
//
//		}
//
//
//












	}
}
